﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using XMLGener.Utils;

namespace XMLGener.Data
{
    /// <summary>
    /// XModuleParam class reperesents one piece of module settings.
    /// </summary>
    public class XModuleParam
    {
        /// <summary>
        /// Create instance of XModuleParam
        /// </summary>
        /// <param name="xml">Input settgins in XML format</param>
        /// <returns>instance of XModuleParam</returns>
        public static XModuleParam instanceFromXML(XmlElement xml)
        {
            XModuleParam result = new XModuleParam();
            XmlElement nameEl = XMLUtils.getSubElement(xml, "name");
            XmlElement typeEl = XMLUtils.getSubElement(xml, "type");
            XmlElement idEl = XMLUtils.getSubElement(xml, "id");
            Dictionary<string, XPairClass> itemsList = new Dictionary<string, XPairClass>();
            XmlElement items = XMLUtils.getSubElement(xml, "items");
            if (items != null) items = XMLUtils.getSubElement(items, "item");
            while (items != null)
            {
                string name = XMLUtils.getAttribute(items, "name", "");
                string value = XMLUtils.getAttribute(items, "value", "");
                if (string.IsNullOrEmpty(name))
                    name = value;

                if (!string.IsNullOrEmpty(value))
                {
                    if (!itemsList.ContainsKey(value))
                        itemsList.Add(value, new XPairClass(name,value));
                }
                items = XMLUtils.getNextElement(items, "item");
            }

            if (idEl == null)
            {
                XmlElement modElemnt = null;
                if (xml.ParentNode != null && xml.ParentNode.ParentNode != null)
                {
                    modElemnt = (XmlElement)xml.ParentNode.ParentNode;
                    modElemnt = XMLUtils.getSubElement(modElemnt,"name");
                }
                if (modElemnt!=null)
                    XLogger.addError("XModuleParam.parseFromXML", "In module " + modElemnt.InnerText+ " is missing id of attriute.");
                else
                    XLogger.addError("XModuleParam.parseFromXML", "In undefined module is missing id of attribute.");
                return null;
            }
            result.id = idEl.InnerText;
            if (nameEl != null) 
                result.name = nameEl.InnerText;
            if (typeEl != null) 
                result.type = typeEl.InnerText;
            result.itemsList = itemsList;
            return result;
        }

        /// <summary>
        /// Module name
        /// </summary>
        public string name = "";
        
        /// <summary>
        /// module id = name of attribute in settings
        /// </summary>
        public string id = "missing";
        

        /// <summary>
        /// Output type
        /// </summary>
        public string type = "any";

        /// <summary>
        /// List of items, where each item is composed from name and value.
        /// Value is key of item.
        /// </summary>
        public Dictionary<string, XPairClass> itemsList = new Dictionary<string, XPairClass>();
    }
}
